package cn.com.guage.dtm.mapping.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.mapping.domain.MappingNumberRule;
import cn.com.guage.dtm.mapping.service.IMappingNumberRuleService;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.common.core.page.TableDataInfo;

/**
 * 编号规则Controller
 * 
 * @author liujun
 * @date 2021-07-14
 */
@Controller
@RequestMapping("/mapping/norule")
public class MappingNumberRuleController extends BaseController
{
    private String prefix = "mapping/norule";

    @Autowired
    private IMappingNumberRuleService mappingNumberRuleService;

    @RequiresPermissions("mapping:norule:view")
    @GetMapping()
    public String norule()
    {
        return prefix + "/norule";
    }

    /**
     * 查询编号规则列表
     */
    @RequiresPermissions("mapping:norule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MappingNumberRule mappingNumberRule)
    {
        startPage();
        List<MappingNumberRule> list = mappingNumberRuleService.selectMappingNumberRuleList(mappingNumberRule);
        return getDataTable(list);
    }

    /**
     * 导出编号规则列表
     */
    @RequiresPermissions("mapping:norule:export")
    @Log(title = "编号规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MappingNumberRule mappingNumberRule)
    {
        List<MappingNumberRule> list = mappingNumberRuleService.selectMappingNumberRuleList(mappingNumberRule);
        ExcelUtil<MappingNumberRule> util = new ExcelUtil<MappingNumberRule>(MappingNumberRule.class);
        return util.exportExcel(list, "编号规则数据");
    }

    /**
     * 新增编号规则
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存编号规则
     */
    @RequiresPermissions("mapping:norule:add")
    @Log(title = "编号规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MappingNumberRule mappingNumberRule)
    {
        return toAjax(mappingNumberRuleService.insertMappingNumberRule(mappingNumberRule));
    }

    /**
     * 修改编号规则
     */
    @GetMapping("/edit/{noRuleId}")
    public String edit(@PathVariable("noRuleId") Long noRuleId, ModelMap mmap)
    {
        MappingNumberRule mappingNumberRule = mappingNumberRuleService.selectMappingNumberRuleById(noRuleId);
        mmap.put("mappingNumberRule", mappingNumberRule);
        return prefix + "/edit";
    }

    /**
     * 修改保存编号规则
     */
    @RequiresPermissions("mapping:norule:edit")
    @Log(title = "编号规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MappingNumberRule mappingNumberRule)
    {
        return toAjax(mappingNumberRuleService.updateMappingNumberRule(mappingNumberRule));
    }

    /**
     * 删除编号规则
     */
    @RequiresPermissions("mapping:norule:remove")
    @Log(title = "编号规则", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mappingNumberRuleService.deleteMappingNumberRuleByIds(ids));
    }
}
